package com.xqh.service.impl.pms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xqh.base.common.TreeEntity;
import com.xqh.dao.pms.SysResourceMapper;
import com.xqh.entity.pms.SysResource;
import com.xqh.entity.pms.SysRole;
import com.xqh.enums.RoleTypeEnums;
import com.xqh.service.pms.SysResourceService;
import javax.annotation.Resource;

import com.xqh.service.pms.SysRoleService;
import com.xqh.base.dao.CrudMapper;
import com.xqh.base.exception.ServiceException;
import com.xqh.base.service.impl.BaseCrudServiceImpl;
import com.xqh.base.util.id.IdWorker;
import com.xqh.base.util.serializer.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author xqh
 * @date 2017-11-03 17:24:06
 * @version 1.0.0
 * @copyright www.xqh.com
 */
@Service("sysResourceService")
public class SysResourceServiceImpl extends BaseCrudServiceImpl<SysResource> implements SysResourceService {

    @Resource
    private SysResourceMapper sysResourceMapper;

    @Autowired
    private SysRoleService roleService;

    @Override
    public CrudMapper init() {
        return sysResourceMapper;
    }

    @Override
    public List<SysResource> getResourceByRole(String roleCode) {
        Map<String,Object> params=new HashMap<>();
        params.put("code",roleCode);
        SysRole role=roleService.findOneByBiz(params);
        if(null != role){
            Map<String,Object> map=new HashMap<>();
            if(!role.getRoleType().equals(RoleTypeEnums.SUPER_ADMIN.toString())){
                //非超级管理员
                map.put("roleCode",roleCode);
            }
            List<SysResource> resourceList=sysResourceMapper.selectByParams(map);
            return resourceList;
        }
        return null;
    }

    /**
     * 获取树结构数据
     * @param params
     * @return
     */
    @Override
    public JSONArray getTreeData(SysResource params){
        List<SysResource> resourceDtoList=sysResourceMapper.selectByParams(ObjectUtils.changeToMap(params));
        JSONArray resArray = ObjectUtils.wrapperGetTreeData(resourceDtoList);

        if(null != params && StringUtils.hasText(params.getRefRoleCode())){
            //这里需要查出 角色绑定的所有资源
            List<SysResource> exists=getResourceByRole(params.getRefRoleCode());
            ObjectUtils.wrapperCheckTreeData(resArray, ObjectUtils.copyListObject(exists, TreeEntity.class));
        }
        return ObjectUtils.setTreeHomeNode(resArray);
    }

    @Override
    @Transactional
    public int saveData(SysResource entity) throws ServiceException {
        Assert.notNull(entity);
        Assert.notNull(entity.getIsMenu());
        if(null != entity.getId()){

            //修改数据
            SysResource old=this.findById(entity.getId());
            Assert.notNull(old,"找不到资源数据");
            old.setName(entity.getName());
            old.setOrders(entity.getOrders());
            old.setUrl(entity.getUrl());
            old.setIconCode(entity.getIconCode());
            old.setRemark(entity.getRemark());
            old.setPermission(entity.getPermission());
            return this.sysResourceMapper.updateByPrimaryKey(old);
        }else{
            //新增
            entity.setCode(IdWorker.getStringCode());
            entity.setStatus(1);
            if(!StringUtils.isEmpty(entity.getParentCode())){
                //有上级资源
                Map<String,Object> param=new HashMap<>();
                param.put("code",entity.getParentCode());
                List<SysResource> plist=this.findByBiz(param);
                Assert.isTrue(!CollectionUtils.isEmpty(plist),"上级资源编码有误");
                SysResource parent=plist.get(0);
                if(entity.getIsMenu() == 1){//如果是添加菜单数据
                    Assert.isTrue(parent.getLevel() == 1,"菜单只能添加两级");//目前只允许添加两级菜单
                }
                entity.setLongCode(parent.getLongCode()+","+entity.getCode());
                entity.setLevel(parent.getLevel()+1);
            }else{
                entity.setLevel(1);//没有上级 就是一级菜单
                entity.setLongCode(entity.getCode());
            }
            return sysResourceMapper.insert(entity);
        }
    }

    @Override
    @Transactional
    public int deleteById(Long id) throws ServiceException {
        int childCount=sysResourceMapper.checkChildCount(id);
        Assert.isTrue(childCount==0,"包含下级数据不能删除");
        return sysResourceMapper.deleteByPrimaryKey(id);
    }
}